<script type="text/javascript">
    function bo_datatables_sel_all(elm) {
        var b = $(elm).prop("checked");
        var table_id = $(elm).data('table-id');
        $("#"+table_id).find("input.bo_datatables_row_checkbox").each(function() {
            $(this).prop("checked", b);
        });
    }
    var {{table_id}}_datatables = null;
    var {{table_id}}_js_datatables = null;
    var {{table_id}}_bar_actions = {% raw json_encode(provider.dropdown_actions(handler)) %};
    var {{table_id}}_row_actions = {% raw json_encode(provider.row_actions(handler)) %};
    {% set pk = provider.primary_key(handler) %}
    $(document).ready(function() {
        {{table_id}}_js_datatables = new BoListTable('{{table_id}}', {{table_id}}_datatables, {{table_id}}_bar_actions, {{table_id}}_row_actions, '{{pk}}', '{{reverse_url('listtable', api_name)}}');
        {{table_id}}_datatables = $('#{{table_id}}').on('init.dt', function(){
            {{table_id}}_js_datatables.bindRowAction();
        }).DataTable({
            "processing": true,
            "ordering": true,
            "serverSide": true,
            "info": true,
            responsive: true,
            "language": '',
            "sDom": "<'row'<'col-sm-8 bo_datatables_actions_bar'><'col-sm-4'f>r>t<'row'<'col-sm-8'i><'col-sm-4'p>>",
            "ajax": {
                url: "{{reverse_url('listtable', api_name)}}",
                type: "GET",
                "data": function(params) {
                    {% set custom_ajax_params_cb = provider.ajax_params_cb() %}
                    {% if custom_ajax_params_cb %}
                    {{custom_ajax_params_cb}}(params);
                    {% end %}
                    return bo_xsrf({"params":JSON.stringify(params)});
                }
            },
            oLanguage: {
                "sProcessing": "数据加载中……",
                "sSearch": "快速检索:",
                "sLengthMenu": "每页显示 _MENU_ 条",
                "sInfo": "从 _START_ 到 _END_ 条，总共 _TOTAL_ 条记录",
                "sZeroRecords": "没有记录",
                "sInfoEmpty": "暂无记录",
                "sInfoFiltered": "(一共有 _MAX_  条记录)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": " 上一页 ",
                    "sNext": " 下一页 ",
                    "sLast": " 末页 "
                }
            },
            "columns": [
            {% set cols = provider.columns(handler) %}
            {% set col_index = 0 %}

            {% if pk %}
            {
                "data": "{{pk}}"
            },
            {% end %}
            {% for field_name in cols %}
            {
                "data": "{{field_name}}"
            }
            {% if col_index < len(cols)-1 %}
            ,
            {% end %}
            {% set col_index += 1 %}
            {% end %}
            ],
            "columnDefs": [
            {% set col_index = 0 %}
            {% set extra_index = 0 %}
            {% if pk %}
            {
                "targets": {{col_index}},
                "render": function(data, type, row) {
                    return '<input type="checkbox" class="bo_datatables_row_checkbox"  value="'+data+'" name="{{pk}}"/>';
                },
                "searchable": false,
                "orderable": false,
            },
            {% set col_index += 1 %}
            {% set extra_index = 1 %}
            {% end %}

            {% set custom_column_render_function_name = provider.column_render_cb() %}
            {% for field_name in cols %}
            {
                //{{field_name}}
                "render": function(data, type, row) {
                var style_text = data;
                {% if custom_column_render_function_name %}
                style_text = {{custom_column_render_function_name}}('{{field_name}}', data, type, row);
                {% end %}
                var row_action = {{table_id}}_row_actions['{{field_name}}']
                if(row_action!=undefined){
                    style_text = {{table_id}}_js_datatables.buildActionsRow(style_text, row, row_action);
                }
                return style_text;
                },
                //searchable columns
                {% if field_name in provider.searchable_columns(handler) %}
                "searchable": true,
                {% else %}
                "searchable": false,
                {% end %}
                //orderable columns
                {% if field_name in provider.orderable_columns(handler) %}
                "orderable": true,
                {% else %}
                "orderable": false,
                {% end %}
                //visible columns
                {% if field_name in provider.hide_columns(handler) %}
                "visible": false,
                {% else %}
                "visible": true,
                {% end %}
                "targets": {{col_index}}
            }
            {% if col_index < len(cols)-(1-extra_index) %}
            ,
            {% end %}
            {% set col_index += 1 %}
            {% end %}
            ]
        });
        {{table_id}}_js_datatables.dataTables = {{table_id}}_datatables;
        {{table_id}}_js_datatables.init();
    });

</script>